# Python 2.6.4
# Project Euler, Problem 66
# Copyright 2010 Talha Zaman

from math import sqrt
def cfc(D):
    a0 = int(sqrt(D))
    if a0**2==D: return 0
    P0, Q0 = 0, 1
    chain, conv = [a0], [(a0, 1)]
    i, found = 0, False
    while True:
        i += 1
        P1 = a0*Q0-P0
        Q1 = int((D-P1**2)/Q0)
        a1 = int((sqrt(D)+P1)/Q1)
        if not found: chain.append(a1)
        if i>1:
            p1 = a1*conv[i-1][0] + conv[i-2][0]
            q1 = a1*conv[i-1][1] + conv[i-2][1]
            conv.append((p1,q1))
        else: conv.append((a0*a1+1, a1))
        if not found and a1==2*chain[0]: r, found = i-1, True
        if found:
            if r%2: return conv[r][0]
            elif i>2*r: return conv[2*r+1][0]
        P0, Q0, a0 = P1, Q1, a1

xmax, imax = 0, 0
for i in range(2, 1001):
    x = cfc(i)
    if xmax<x: xmax, imax = x, i
print imax
